from utils.excel_util import ExcelUtil
from model.sysJobModel import SysJob
from schema.sysJobSchema import SysJobCreate
from typing import List, Any, Dict, ClassVar
from io import BytesIO
from datetime import datetime


class SysJobExcel:
    # Define export columns
    COLUMNS: ClassVar[List[Dict[str, Any]]] = [

        {"field": "invoke_target", "title": "调用目标字符串", "required": True},

        {"field": "cron_expression", "title": "cron执行表达式", "required": False},

        {"field": "misfire_policy", "title": "计划执行错误策略（1立即执行 2执行一次 3放弃执行）", "required": False},

        {"field": "concurrent", "title": "是否并发执行（0允许 1禁止）", "required": False},

        {"field": "job_status", "title": "任务状态（0正常 1暂停）", "required": False},

        {"field": "status", "title": "状态（0正常 2删除）", "required": False},

        {"field": "create_by", "title": "创建者", "required": False},

        {"field": "create_time", "title": "创建时间", "required": False},

        {"field": "update_by", "title": "更新者", "required": False},

        {"field": "update_time", "title": "更新时间", "required": False},

        {"field": "remark", "title": "备注信息", "required": False},

    ]

    @classmethod
    def get_export_columns(cls) -> List[Dict[str, Any]]:
        return [

            {"field": "invoke_target", "title": "调用目标字符串"},

            {"field": "cron_expression", "title": "cron执行表达式"},

            {"field": "misfire_policy", "title": "计划执行错误策略（1立即执行 2执行一次 3放弃执行）"},

            {"field": "concurrent", "title": "是否并发执行（0允许 1禁止）"},

            {"field": "job_status", "title": "任务状态（0正常 1暂停）"},

            {"field": "status", "title": "状态（0正常 2删除）"},

            {"field": "create_by", "title": "创建者"},

            {"field": "create_time", "title": "创建时间"},

            {"field": "update_by", "title": "更新者"},

            {"field": "update_time", "title": "更新时间"},

            {"field": "remark", "title": "备注信息"},

        ]

    @classmethod
    def get_import_template(cls) -> BytesIO:
        """Get import template"""
        example_data = [{

            "invoke_target": "example",

            "cron_expression": "example",

            "misfire_policy": "example",

            "concurrent": "A",

            "job_status": "A",

            "status": "A",

            "create_by": "example",

            "create_time": datetime.now(),

            "update_by": "example",

            "update_time": datetime.now(),

            "remark": "example",

        }]
        return ExcelUtil.create_import_template(
            columns=cls.COLUMNS,
            example_data=example_data,
            sheet_name="定时任务调度表导入模板"
        )

    @classmethod
    async def export_data(cls, data: List[Any]) -> BytesIO:
        """Export data"""
        return await ExcelUtil.export_data(
            data=data,
            columns=cls.get_export_columns(),
            sheet_name="定时任务调度表数据"
        )

    @classmethod
    def parse_import_data(cls, file: BytesIO) -> List[Dict[str, Any]]:
        """Parse import data"""
        return ExcelUtil.parse_import_data(
            file=file,
            columns=cls.COLUMNS,
            schema_cls=SysJobCreate
        )